package com.mall.book.controller;

import com.alibaba.fastjson.JSON;
import com.mall.book.entity.po.OrderInfo;
import com.mall.book.entity.po.OrderStatus;
import com.mall.book.entity.po.SubOrderInfo;
import com.mall.book.service.OrderInfoService;
import com.mall.book.service.OrderStatusService;
import com.mall.book.service.SubOrderInfoService;
import com.mall.book.wrapper.ResultWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Dizziness on 2022/4/17 22:47
 */
@Controller
public class BusinessCenter {
    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    SubOrderInfoService subOrderInfoService;

    @Autowired
    OrderStatusService orderStatusService;

    /**
     * 单独查一个父订单的详细情况
     */
    @PostMapping("/bussiness/getDetails")
    @ResponseBody
    public ResultWrapper getDetails(HttpServletRequest request){
        String father_id = request.getParameter("id");

        //子订单信息展示：商品信息，订单状态，之前的状态
        Collection<SubOrderInfo> children = subOrderInfoService.getByFatherIdEvenDeleted(father_id);

        ArrayList<Map> result = new ArrayList<>();
        for (SubOrderInfo child : children) {
            HashMap<String, Object> wrapper = new HashMap<>();

            wrapper.put("info", child);

            List<OrderStatus> statuses = orderStatusService.getByIdEvenDeleted(child.getSubOrderId());
            wrapper.put("statuses", statuses);

            int current_state = 0;
            for (OrderStatus status : statuses) {
                current_state = Math.max(current_state,status.getOrderStatusCode());
            }
            wrapper.put("current_state", current_state);

            result.add(wrapper);
        }
        return ResultWrapper.builder().code(200).msg("获取订单详细数据成功").data(result).build();
    }

    /**
     * 获取父订单列表
     * 功能：以父订单为根返回订单集合，前端可以用关键信息再请求获取更详细的信息
     */
    @PostMapping("/business/getOrders")
    @ResponseBody
    public ResultWrapper getOrders(HttpServletRequest request) {
        long size = Long.valueOf(request.getParameter("size"));
        long current = Long.valueOf(request.getParameter("current"));

        Map<String,String> selector = (Map<String, String>) JSON.parse(request.getParameter("selector"));
        if(selector != null) {
            selector.keySet().iterator().forEachRemaining(a->{
                System.out.println(a + ":" + selector.get(a));
            });
        }
        Collection<OrderInfo> infos = orderInfoService.getPageEvenDeleted(selector, current, size);

        int total = orderInfoService.getTotal();

        HashMap<String, Object> result = new HashMap<>();
        result.put("orders", infos);
        result.put("total", total);

        return ResultWrapper.builder().code(200).msg("分页查询第" + current + "页的" + size + "条数据成功").data(result).build();
    }
}
